Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Text
Imports System.Data
Imports System.Collections.Generic
Imports System.Xml

Public Class InventoryItem

    Private Const XMLPath As String = "~/App_Data/Inventory.xml"
    Private Const XMLHistoryPath As String = "~/App_Data/InventoryHistory.xml"

#Region " Properties"

    Public Property ID() As Guid
    Public Property Description() As String
    Public Property PartNumber() As String
    Public Property MFGNumber() As String
    Public Property VendorID() As Guid
    Public Property Notes() As String
    Public Property ReorderPoint() As Integer
    Public Property Quantity() As Integer
    
#End Region
#Region " Public Methods"

    Public Shared Function Search(ByVal desc As String, ByVal partnum As String, ByVal mfgnum As String, ByVal vendorid As Guid) As List(Of InventoryItem)
        Dim inventorylist As New List(Of InventoryItem)
        For Each i As InventoryItem In FetchList()
            'Dim _added As Boolean = False
            If desc.Trim.Length > 0 Then
                If i.Description.Contains(desc) Then
                    If Not inventorylist.Contains(i) Then inventorylist.Add(i)
                End If
            End If
            If partnum.Trim.Length > 0 Then
                If i.PartNumber.ToLower = partnum.ToLower Then
                    If Not inventorylist.Contains(i) Then inventorylist.Add(i)
                End If
            End If
            If mfgnum.Trim.Length > 0 Then
                If i.MFGNumber.ToLower = mfgnum.ToLower Then
                    If Not inventorylist.Contains(i) Then inventorylist.Add(i)
                End If
            End If
            If Not vendorid = Guid.Empty Then
                If i.VendorID = vendorid Then
                    If Not inventorylist.Contains(i) Then inventorylist.Add(i)
                End If
            End If
            If desc.Trim.Length = 0 AndAlso partnum.Trim.Length = 0 AndAlso mfgnum.Trim.Length = 0 AndAlso vendorid = Guid.Empty Then
                If Not inventorylist.Contains(i) Then inventorylist.Add(i)
            End If
        Next
        Return inventorylist
    End Function
    Public Shared Function Delete(ByVal id As Guid, ByRef msgOut As String) As Boolean
        Return DeleteInventoryItem(id, msgOut)
    End Function


    Public Shared Function FetchReorderList() As List(Of InventoryItem)
        Dim inventorylist As New List(Of InventoryItem)
        For Each i As InventoryItem In FetchList()
            If i.ReorderPoint >= i.Quantity Then
                inventorylist.Add(i)
            End If
        Next
        Return inventorylist
    End Function
    Public Shared Function FetchList() As List(Of InventoryItem)
        Dim inventorylist As New List(Of InventoryItem)
        Dim xdoc As New XmlDocument
        xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
        For Each n As XmlNode In xdoc("Items").ChildNodes
            inventorylist.Add(ConvertToObject(n))
        Next
        Return inventorylist
    End Function
    Public Shared Function Fetch(ByVal id As Guid) As InventoryItem
        Dim inv As New InventoryItem
        For Each i As InventoryItem In FetchList()
            If i.ID = id Then
                inv = i
                Exit For
            End If
        Next
        Return inv
    End Function
    Public Shared Function Save(ByVal i As InventoryItem) As Boolean
        If Not i.ID = Nothing Then
            ' update
            Return Update(i)
        Else
            ' insert 
            Return Insert(i)
        End If
    End Function
    Private Shared Function ConvertToObject(ByVal n As XmlNode) As InventoryItem
        Dim _inventory As New InventoryItem
        With _inventory
            .ID = New Guid(n("ID").InnerText)
            .Description = n("Description").InnerText
            .MFGNumber = n("MFGNumber").InnerText
            .Notes = n("Notes").InnerText
            .PartNumber = n("PartNumber").InnerText
            .Quantity = CInt(n("Quantity").InnerText)
            .ReorderPoint = CInt(n("ReorderPoint").InnerText)
            .VendorID = New Guid(n("VendorID").InnerText)
        End With
        Return _inventory
    End Function
    Private Shared Function Insert(ByVal i As InventoryItem) As Boolean
        Dim _return As Boolean = False
        Try
            i.ID = Guid.NewGuid
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
            Dim n As XmlNode = xdoc("Items")
            Dim newNode As XmlNode = xdoc.CreateNode(XmlNodeType.Element, "Item", Nothing)
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "ID", Nothing)).InnerText = i.ID.ToString
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Description", Nothing)).InnerText = i.Description
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "MFGNumber", Nothing)).InnerText = i.MFGNumber
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Notes", Nothing)).InnerText = i.Notes
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "PartNumber", Nothing)).InnerText = i.PartNumber
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Quantity", Nothing)).InnerText = i.Quantity
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "ReorderPoint", Nothing)).InnerText = i.ReorderPoint
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "VendorID", Nothing)).InnerText = i.VendorID.ToString
            n.AppendChild(newNode)
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLPath))
            _return = True
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
        Finally
            If _return = True Then AddHistory(i.ID, i.Quantity)
        End Try
        Return _return

    End Function
    Private Shared Function Update(ByVal i As InventoryItem) As Boolean
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
            For Each n As XmlNode In xdoc("Items").ChildNodes
                If New Guid(n("ID").InnerText) = i.ID Then
                    n("Description").InnerText = i.Description
                    n("MFGNumber").InnerText = i.MFGNumber
                    n("Notes").InnerText = i.Notes
                    n("PartNumber").InnerText = i.PartNumber
                    If n("Quantity").InnerText <> i.Quantity Then AddHistory(i.ID, (i.Quantity - CInt(n("Quantity").InnerText)))
                    n("Quantity").InnerText = i.Quantity
                    n("ReorderPoint").InnerText = i.ReorderPoint
                    n("VendorID").InnerText = i.VendorID.ToString
                    Exit For
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLPath))
            _return = True
        Catch ex As Exception
            'log error
        End Try
        Return _return
    End Function
    Public Shared Function DeleteInventoryItem(ByVal id As Guid, ByRef msgOut As String) As Boolean
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
            For Each n As XmlNode In xdoc("Items").ChildNodes
                If New Guid(n("ID").InnerText) = id Then
                    xdoc("Items").RemoveChild(n)
                    Exit For
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLPath))
            _return = True
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
            msgOut += vbCrLf & "Delete Error: " & ex.Message
            _return = False
        End Try
        Return _return
    End Function
    Public Shared Sub AddHistory(ByVal id As Guid, ByVal qty As Integer)
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLHistoryPath))
            Dim n As XmlNode = xdoc("Items")
            Dim newNode As XmlNode = xdoc.CreateNode(XmlNodeType.Element, "Item", Nothing)
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "ID", Nothing)).InnerText = id.ToString
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "QtyChange", Nothing)).InnerText = qty.ToString
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "User", Nothing)).InnerText = HttpContext.Current.User.Identity.Name
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Date", Nothing)).InnerText = Now.ToString()
            n.AppendChild(newNode)
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLHistoryPath))
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
        End Try
    End Sub
    Public Shared Function GetHistory(ByVal id As Guid) As List(Of ItemHistory)
        Dim _return As New List(Of ItemHistory)
        Dim xdoc As New XmlDocument
        xdoc.Load(HttpContext.Current.Server.MapPath(XMLHistoryPath))
        For Each n As XmlNode In xdoc("Items").ChildNodes
            If id = New Guid(n("ID").InnerText) Then
                Dim ih As New ItemHistory
                ih.ID = New Guid(n("ID").InnerText)
                ih.Qty = n("QtyChange").InnerText
                ih.UserID = n("User").InnerText
                ih.ChangeDate = CDate(n("Date").InnerText)
                _return.Add(ih)
            End If
        Next
        _return = _return.OrderBy(Function(item) CDate(item.ChangeDate)).ToList()
        _return.Reverse()
        Return _return
    End Function
#End Region

    Public Class ItemHistory
        Public Property ID As Guid
        Public Property Qty As Integer
        Public Property UserID As String
        Public Property ChangeDate As DateTime
    End Class
End Class

